package cn.yqn.org.controller;

import cn.yqn.basic.annotation.PreAuthorize;
import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.org.domain.Employee;
import cn.yqn.org.query.EmployeeQuery;
import cn.yqn.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: 查询全部
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: []
    * @Return java.util.List<cn.yqn.org.domain.Employee>
    */
    @GetMapping
    @PreAuthorize(name = "员工查询全部",sn ="emp:get")
    public List<Employee> loadAll() {
        return employeeService.loadAll();
    }

    /**
    * @Title: 根据id查询
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [id]
    * @Return cn.yqn.org.domain.Employee
    */
    @GetMapping("/{id}")
    @PreAuthorize(name="员工查询Id", sn="emp:get")
    public Employee loadById(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    /**
    * @Title: 根据id删除
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [id]
    * @Return cn.yqn.basic.util.JsonResult
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name="员工删除", sn="emp:delete")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /** 
    * @Title: 批量删除
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:03  
    * @Parameters: [ids]
    * @Return cn.yqn.basic.util.JsonResult
    */
    @PatchMapping
    @PreAuthorize(name="员工批量清除", sn="emp:patch")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDel(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
    * @Title: 新增或修改
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [employee]
    * @Return cn.yqn.basic.util.JsonResult
    */
    @PutMapping
    @PreAuthorize(name="员工添加修改", sn="emp:put")
    public JsonResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else{
                employeeService.update(employee);
            }
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
    * @Title: 分页展示
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [employeeQuery]
    * @Return cn.yqn.basic.util.PageList<cn.yqn.org.domain.Employee>
    */
    @PostMapping
    @PreAuthorize(name="员工信息展示", sn="emp:post")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        try {
            return employeeService.pageQuery(employeeQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Employee>();
        }
    }
    /**
     * 根据shopId查询所有的店蒲员工
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "根据shopid查询" )
    public List<Employee> findByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.findByShopId(shopId);
    }

}
